<!--
 * @Description: 02-在数组开头插入元素
 * @Autor: wangxin
 * @Date: 2020-06-08 20:52:31
 * @LastEditors: Seven
 * @LastEditTime: 2020-06-08 20:59:28
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>在数组开头插入元素</title>
  </head>
  <body>
    <script>
      Array.prototype.insertFirstElement = function (value) {
        for (let i = this.length; i >= 0; i--) {
          this[i] = this[i - 1] // 所有元素向右移动一位
        }

        this[0] = value
      }

      // 测试代码
      const arr = [1, 2, 3, 4, 5]
      arr.insertFirstElement(0)
      console.log(arr) // [0, 1, 2, 3, 4, 5]
    </script>
  </body>
</html>
